package com.zdkj.subway.rca.service.base;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zdkj.subway.rca.dto.SwCkReportReviewExportDTO;
import com.zdkj.subway.rca.entity.CodeName;
import com.zdkj.subway.rca.entity.SwCkReportReview;
import com.zdkj.subway.rca.mapper.SwCkReportReviewMapper;
import com.zdkj.subway.rca.mapper.SysDictCommonMapper;
import com.zdkj.subway.rca.service.SwCkReportReviewService;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author cooze
 * @version 1.0.0 创建于 2020/10/27
 **/
public abstract class SwCkReportReviewServiceBase extends ServiceImpl<SwCkReportReviewMapper, SwCkReportReview> implements SwCkReportReviewService {

    public String excelSheetName() {
        return "报表间数据关联审核";
    }

    public String exportFileName() throws Exception {
        return String.format("attachment;filename=%s.xlsx", (URLEncoder.encode(excelSheetName(), "UTF-8") + System.currentTimeMillis()));
    }

    public List<SwCkReportReview> queryExportDataResult(SwCkReportReviewExportDTO dto) {

        SwCkReportReview swCkReportReview = new SwCkReportReview();
        if (StringUtils.isNotEmpty(dto.getDiffType())) {
            swCkReportReview.setDifferenceType(dto.getDiffType());
        }
        boolean isContainMainReport = StringUtils.isNotEmpty(dto.getMainReportStartDate()) && StringUtils.isNotEmpty(dto.getMainReportEndDate());
        boolean isContainRecordDiff = StringUtils.isNotEmpty(dto.getRecordDiffStartDate()) && StringUtils.isNotEmpty(dto.getRecordDiffEndDate());
        boolean isContainMoney = ObjectUtils.isNotNull(dto.getStartMoney()) && ObjectUtils.isNotNull(dto.getEndMoney());
        List<SwCkReportReview> list = baseMapper.selectList(
                Wrappers.query(swCkReportReview).lambda()
                        .between(isContainMainReport, SwCkReportReview::getMainReportCreateTime, dto.getMainReportStartDate(), dto.getMainReportEndDate())
                        .between(isContainRecordDiff, SwCkReportReview::getRecordDifferenceTime, dto.getRecordDiffStartDate(), dto.getRecordDiffEndDate())
                        .between(isContainMoney, SwCkReportReview::getDifferenceAmount, dto.getStartMoney(), dto.getEndMoney())
        );
        if (CollectionUtils.isEmpty(list)) {
            return new LinkedList<>();
        }
        Map<String, String> map = new HashMap<>();
        diffType().forEach(codeName -> map.put(codeName.getCode(), codeName.getName()));
        list.forEach(rr -> rr.setDifferenceTypeName(map.getOrDefault(rr.getDifferenceType(), "")));
        return list;
    }


    public abstract SysDictCommonMapper getSysDictCommonMapper();

    @Override
    public List<CodeName> diffType() {
        return getSysDictCommonMapper().selectAllDictKv("sw_ck_report_review_diff_type");
    }

}
